import { BaseController } from '@/api/BaseController';
import { RequestOption } from '@/common/http/types';
import { ANY_OBJECT } from '@/types/generic';
import { TableData } from '@/common/types/table';
import { OnlineApi } from '@/types/online/script';
import { API_CONTEXT } from '../config';

/**
 * 在线表单API管理接口
 */
export default class OnlineApiController extends BaseController {
  /**
   * 模拟Api运行
   */
  static testScript(params: ANY_OBJECT, httpOptions?: RequestOption) {
    return super.post<ANY_OBJECT>(
      API_CONTEXT + '/online/onlineApi/testScript',
      params,
      httpOptions,
    );
  }

  /**
   * Jie. 20250717 通过ApiCode运行
   */
  static runScript(params: ANY_OBJECT, httpOptions?: RequestOption) {
    const { scriptCode } = params;
    if (scriptCode == null || scriptCode === '')
      return Promise.reject(new Error('脚本编码不能为空'));
    const url = API_CONTEXT + '/online/onlineApi/runScript/' + scriptCode;
    return super.post<ANY_OBJECT>(url, params, httpOptions);
  }

  /**
   * 列出符合过滤条件的在线表单API列表
   */
  static list(params: ANY_OBJECT, httpOptions?: RequestOption) {
    return super.post<TableData<OnlineApi>>(
      API_CONTEXT + '/online/onlineApi/list',
      params,
      httpOptions,
    );
  }

  /**
   * 查看指定在线表单API对象详情
   */
  static view(params: ANY_OBJECT, httpOptions?: RequestOption) {
    return super.get<OnlineApi>(API_CONTEXT + '/online/onlineApi/view', params, httpOptions);
  }

  /**
   * 新增在线表单API数据
   */
  static add(params: ANY_OBJECT, httpOptions?: RequestOption) {
    return super.post(API_CONTEXT + '/online/onlineApi/add', params, httpOptions);
  }

  /**
   * 更新在线表单API数据
   */
  static update(params: ANY_OBJECT, httpOptions?: RequestOption) {
    return super.post(API_CONTEXT + '/online/onlineApi/update', params, httpOptions);
  }

  /**
   * 删除在线表单API数据
   */
  static delete(params: ANY_OBJECT, httpOptions?: RequestOption) {
    return super.post(API_CONTEXT + '/online/onlineApi/delete', params, httpOptions);
  }

  /**
   * 批量删除在线表单API数据
   */
  static deleteBatch(params: ANY_OBJECT, httpOptions?: RequestOption) {
    return super.post(API_CONTEXT + '/online/onlineApi/deleteBatch', params, httpOptions);
  }
}
